//
//  MaterialViewController.m
//  SsehMaster
//
//  Created by 杨阳 on 2018/8/11.
//  Copyright © 2018年 yinlong. All rights reserved.
//

#import "MaterialViewController.h"
#import "LabelTableViewCell.h"
#import "SearchMaterialModel.h"

@interface MaterialViewController ()<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UISearchBar * searchBar;

@property (nonatomic,strong) UITableView * tableView;

@property (nonatomic,strong) NSMutableArray * dataSource;

@property (nonatomic,strong) SearchMaterialModel * dataModel;



@end

static NSString * const LabelTableViewCellID = @"LabelTableViewCellID";

@implementation MaterialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self baseSetting];
    [self creatUI];
    [self searchMaterialApi:@""];
}

-(void)baseSetting{
    
    self.navigationItem.title = @"选择材料";
    self.navigationController.navigationBar.translucent = NO;
    
}

-(void)creatUI{
    
    UIView * backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 50)];
    backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView];
    
    UIButton * searchBut = [UIButton buttonWithType:UIButtonTypeCustom];
    [searchBut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [searchBut setTitle:@"搜索" forState:UIControlStateNormal];
    searchBut.titleLabel.font = [UIFont systemFontOfSize:15];
    [searchBut addTarget:self action:@selector(searchButOnClick:) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:searchBut];
    
    [searchBut mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backView.mas_top);
        make.bottom.equalTo(backView.mas_bottom);
        make.width.mas_equalTo(45 * PROPORTION_WIDTH_IPHONE6);
        make.right.equalTo(backView.mas_right).offset(-5);
    }];
    
    self.searchBar = [[UISearchBar alloc]init];
    self.searchBar.barStyle = UIBarStyleDefault;
    self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
    self.searchBar.placeholder = @"请输入材料名称";
    self.searchBar.delegate = self;
    [backView addSubview:self.searchBar];
    
    UITextField *searchField = [self.searchBar valueForKey:@"searchField"];
    searchField.font = [UIFont systemFontOfSize:14];
    [searchField setValue:[UIFont boldSystemFontOfSize:12] forKeyPath:@"_placeholderLabel.font"];
    
    [self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backView.mas_top).offset(5);
        make.bottom.equalTo(backView.mas_bottom).offset(-5);
        make.left.equalTo(backView.mas_left).offset(10);
        make.right.equalTo(searchBut.mas_left).offset(-5);
    }];
    
    [self creatTableView];
}

-(void)searchButOnClick:(UIButton *)button{
    if (kStringIsEmpty(self.searchBar.text)) {
        return;
    }
    [self searchMaterialApi:self.searchBar.text];
}

-(void)creatTableView{
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, VIEW_WIDTH, VIEW_HEIGHT-NAVIGATION_BAR_HEIGHT-50-HOME_INDICATOR_HEIGHT) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor backGroudColorForView];
    [_tableView registerNib:[UINib nibWithNibName:@"LabelTableViewCell" bundle:nil] forCellReuseIdentifier:LabelTableViewCellID];
    [self.view addSubview:_tableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (kArrayIsEmpty(self.dataModel.data)) {
        return 0;
    }
    return self.dataModel.data.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_complentSelect) {
        MaterialInfoModel * model = self.dataModel.data[indexPath.row];
        if (self.type == SelectdMateralTpyeNew) {
            _complentSelect(model,nil);
        }else{
            _complentSelect(model,self.lastIndexPath);
        }
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LabelTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:LabelTableViewCellID];
    
    MaterialInfoModel * model = self.dataModel.data[indexPath.row];
    
    cell.nameLabel.text = model.material_name;
    
    return cell;
}


-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    if (kStringIsEmpty(self.searchBar.text)) {
        return;
    }
    [self searchMaterialApi:self.searchBar.text];
}

-(void)searchMaterialApi:(NSString *)textStr{
    
    [self.searchBar resignFirstResponder];
    [self showLoading];
    NSDictionary * par = @{@"material_name":textStr};
    [[QYNetManager sharedManager]POST:kGetMaintailMaterials parameters:par andApiTyp:SsehApiTypGetOrders SuccessBlock:^(id responseObject) {
        [self hideLoading];
        if ([responseObject[@"status"] isEqualToString:@"ok"]) {
            self.dataModel = [SearchMaterialModel yy_modelWithDictionary:responseObject];
            [self.tableView reloadData];
        }else{
            [self showTipWithTitle:responseObject[@"error"]];
        }
    } Failure:^(NSError *error) {
        [self hideLoading];
        [self showTipWithTitle:SsehPoolNetStr];
    }];
    
}

-(void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    if (self.searchBar != nil) {
        [self.searchBar becomeFirstResponder];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
